extends Base1
@export var attack_radius = 50.0 # 攻击区域半径
@export var detection_radius = 200.0 # 侦察区域半径

var random=RandomNumberGenerator.new()
var myself=null
var RUN_SPEED=100.00
var patrol_points = [Vector2(100, 100), Vector2(300, 100)] # Example patrol points
var current_patrol_point = 0
var delta=get_process_delta_time()
@export var limit=0.5
@export var endPoint:Marker2D
@onready var animated = $"../../Graphics/AnimatedSprite2D"
@onready var monster = $"../.."
var move=Vector2()
func enter():
	pass
func do():
	if myself:
		#这样会瞬移过来
		#monster.position=monster.position.move_toward(myself.position,RUN_SPEED)
		var direction=(myself.position-monster.position).normalized()
		monster.position += direction * RUN_SPEED * delta
		monster.velocity=direction*RUN_SPEED
		if myself.position==monster.position:
			monster.velocity=Vector2(0,0)
		get_parent().change_state(0)
	else:
		patrol(delta)
	monster.move_and_slide()
	pass

func patrol(delta):
	var direction = (patrol_points[current_patrol_point] - monster.position).normalized()
	monster.position += direction * RUN_SPEED * delta
	monster.velocity=direction*RUN_SPEED
	if monster.position.distance_to(patrol_points[current_patrol_point]) < 10:
		current_patrol_point = (current_patrol_point + 1) % patrol_points.size()
		print(current_patrol_point)
	
func _on_detection_area_body_entered(body):
	myself=body

func _on_detection_area_body_exited(body):
	myself=null


func _on_attack_area_body_entered(body):
	get_parent().change_state(1)
	pass


func _on_attack_area_body_exited(body):
	get_parent().change_state(0)
	pass 
